package cn.xiongxf.controller;

import cn.xiongxf.domain.Movie;
import cn.xiongxf.domain.UserMovie;
import cn.xiongxf.service.MovieService;
import cn.xiongxf.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
public class MovieController {
    @Autowired
    private MovieService movieService;

    @Value("${server.port}")  // 从配置文件中获取参数
    private String port;

    /**
     * Movie服务接口
     * @param product
     * @return
     *//*
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Product product){
        try {
            movieService.add(product);
            return new AjaxResult(true,"添加成功，port = " + port);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"添加失败，port = " + port);
        }
    }*/

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/list")
    public List<Movie> findAll(){
        try {
            return movieService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping("/purchase")
    public AjaxResult purchase(@RequestBody UserMovie userMovie){
        try {
            return movieService.purchase(userMovie);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"购买失败!" + e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public BigDecimal queryPrice(@PathVariable Long id){
        return movieService.queryPrice(id);
    }

    @GetMapping("/presented/{id}")
    public void presented(@PathVariable Long id){
        movieService.presented(id);
    }

    @PostMapping("/watch")
    public AjaxResult watch(@RequestBody UserMovie userMovie){
        return movieService.watch(userMovie);
    }

}
